package com.yanqu.road.dao.impl.activity.langyamemorial;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.langyamemorial.LangYaMemorialLight;
import com.yanqu.road.entity.activity.langyamemorial.LangYaMemorialUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LangYaMemorialUserDataDaoImpl extends TempDataObjectDao<LangYaMemorialUserData> {


    public static String getLangYaMemorialUserDataSql = "select * from `t_u_activity_langya_memorial` where `activityId`=? and `userId`=?";
    public LangYaMemorialUserData getLangYaMemorialUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getLangYaMemorialUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateLangYaMemorialUserDataSql = "update `t_u_activity_langya_memorial` set progress=?,buildTimes=?,buildValue=?,score=?,bigRewardTimes=?,commonTimes=?,lightList=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(LangYaMemorialUserData langYaMemorialUserData) {
        langYaMemorialUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, langYaMemorialUserData.getProgress()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaMemorialUserData.getBuildTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaMemorialUserData.getBuildValue()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaMemorialUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaMemorialUserData.getBigRewardTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaMemorialUserData.getCommonTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaMemorialUserData.getLightList())));
        para.put(i++, new DbParameter(Types.INTEGER, langYaMemorialUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaMemorialUserData.getUserId()));
        if (getDb().executeNoneQuery(updateLangYaMemorialUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertLangYaMemorialUserDataSql = "insert into `t_u_activity_langya_memorial`(activityId,userId,progress,buildTimes,buildValue,score,bigRewardTimes,commonTimes,lightList) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(LangYaMemorialUserData langYaMemorialUserData) {
        langYaMemorialUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, langYaMemorialUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaMemorialUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaMemorialUserData.getProgress()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaMemorialUserData.getBuildTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaMemorialUserData.getBuildValue()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaMemorialUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaMemorialUserData.getBigRewardTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaMemorialUserData.getCommonTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaMemorialUserData.getLightList())));
        if (getDb().executeNoneQuery(insertLangYaMemorialUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected LangYaMemorialUserData parseData(ResultSet rs) throws SQLException {
        LangYaMemorialUserData data = new LangYaMemorialUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setProgress(rs.getInt("progress"));
        data.setBuildTimes(rs.getInt("buildTimes"));
        data.setBuildValue(rs.getInt("buildValue"));
        data.setScore(rs.getLong("score"));
        data.setBigRewardTimes(rs.getInt("bigRewardTimes"));
        data.setCommonTimes(rs.getInt("commonTimes"));
        data.setLightList(JSON.parseObject(rs.getString("lightList"), new TypeReference<List<LangYaMemorialLight>>() {}));
        return data;
    }
}
